<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of userAjaxAction
 *
 * @author Administrator
 */
class user_ajaxAction extends BaseAction{
       protected $nologin= array('login','join_us','reg_submit','check_email','test','logout','view');
       public function execute() {
        //$this->view->setLayoutPath('admsssins');

        $this->view->layout = '';
        if ($_SESSION['user'] == '' && !in_array($this->params['action'], $this->nologin)) {
            exit;
        }

        parent::execute();
    }

    public function actionCate(){
        $user    = $this->getSession('user',false);
        $key     = 'user_cate_'.$user['id'];
//        $cache  = MemcacheAction::getCache();
//        $re     = $cache->get($key);
        $re = null;
        if(!$re) {
            $rs = DAO_Finder::find(DB_SORT, array('uid' =>$_SESSION['user']['id']));
            if($rs) {
                foreach ($rs as $k => $value) {
                    $v[]    = $value->getArray();
                }
                $code   = 'ok';
            }else{
                $v  = array();
                $code   = 'error';
            }
            $re = json_encode(array('code' =>$code, 'data' =>$v));
           // $cache->set($key, $re,3600);
        }
        echo $re;
    }

    /**
     * 获取列表
     */
    public function actionUrl() {
        $condtion   = array('uid' => $_SESSION['user']['id']);
        $status     = $this->postVar('status');
        $cate_id     = $this->postVar('cate_id');
        
        if(!empty($status)) {
            $condtion['status'] = $status;
        }

        if(!empty($cate_id)) {
            $condtion['cate_id'] = $cate_id;
        }
//        $key    = md5('user_url_'.$_SESSION['user']['id'].  implode(',', $condtion));
//        $cache  = MemcacheAction::getCache();
//        $re     = $cache->get($key);
        $code   = 'ok';
        $rs = DAO_Finder::daoAll(DB_URL, $condtion);
        if($rs) {
            $re = array();
            foreach ($rs as $k => $value) {
                $tmp    = $value->getArray();
                $tmp['add_time']    =    date('m月d日', $tmp['created']);
                array_push($re, $tmp);
            }
            //$cache->set($key, $re);
        }else{
            $code   = 'error';
        }
    echo json_encode(array('code'=>$code,'data'=>$re));
    }

    //设置分类
    public function actionSetCate(){
        $id = $this->postVar('ids');
        $cate_id = $this->postVar('cate_id');
        
        if(!empty($id)){
            $sql    = "update u22_url set cate_id =$cate_id where uid=".$_SESSION['user']['id'].' and id in('.implode(',', $id).')';
            DAO_Finder::querySQl($sql);
            echo 'ok';
        }
    }

    public function actionSetStatus() {
        $id = $this->postVar('ids');
        $cate_id = $this->postVar('status');

        if(!empty($id)){
            $sql    = "update u22_url set status ='$cate_id' where uid=".$_SESSION['user']['id'].' and id in('.implode(',', $id).')';
            DAO_Finder::querySQl($sql);
            echo 'ok';
        }
    }

    //添加分类
    public function actionCateAdd() {
        $title  = $this->postVar('title');
        $ids    = $this->postVar('ids');
        if(!empty($title) && !empty($ids)) {
            
            $cate   = DAO_Finder::findOne(DB_SORT, array('title' => $title));
            if(!$cate){
                $cate   = new DB_SORT();
                $cate->setTitle($title);
                $cate->setUid($_SESSION['user']['id']);
                $cate->save();
            }
            if(is_array($ids)) {
                $ids    = implode(',', $ids);
            }
            $sqlQuery   = "update u22_url set cate_id =".$cate->getId()."  where id in($ids)";
            DAO_Finder::exec($sqlQuery,DB_USER);
            echo json_encode(array('code' =>'ok','data' =>$cate->getArray(),'is_new' => $cate->isNew()));
        }else{
            echo json_encode(array('code' =>'error','data' =>'不能为空'));
        }
    }
}
?>
